//
//  YKEssenseController.m
//  百思不得姐
//
//  Created by Eric.Wu on 16/2/24.
//  Copyright © 2016年 Eric.Wu. All rights reserved.
//

#import "YKEssenseController.h"
#import "YKNavigationController.h"
#import <Photos/Photos.h>
#import "YKRecommendTagController.h"
#import "YKAllViewController.h"
#import "YKVideoViewController.h"
#import "YKPictureViewController.h"
#import "YKVoiceViewController.h"
#import "YKWordViewController.h"

@interface YKEssenseController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UITableView *tabeleview;
@property (nonatomic,weak)UIView *indicatorView;
@property (nonatomic,weak)UIView *titleView;
@property (nonatomic,weak)UIButton *SelectButton;
@property (nonatomic,weak)UIScrollView  *contentView;
@end


@implementation YKEssenseController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
 
    [self setupNav];
    
    [self setupChildVC];
#pragma mark - 设置标题栏
    [self setupTitleView];
    //设置内容view
    [self setupContentView];
    
    
   }
#pragma mark - 设置标题栏
- (void)setupTitleView
{
    NSArray *array = @[@"全部",@"视频",@"声音",@"图片",@"段子"];
    NSInteger count = array.count;
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, YkScreeWidth,35)];
     [self.view addSubview:titleView];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.y = titleView.height - indicatorView.height;
    self.indicatorView = indicatorView;
    CGFloat width = (YkScreeWidth / count);
    
    for (int i = 0; i< count; i++) {
        
        UIButton *btn = [[UIButton alloc] init];

        btn.tag = i;
        btn.x = i *width;
        btn.width = width;
        btn.height = titleView.height;
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:btn];

        if (i == 0) {
            btn.enabled = NO;
            self.SelectButton = btn;
    
            [btn.titleLabel sizeToFit];
            self.indicatorView.width = btn.titleLabel.width;
            self.indicatorView.centerX = btn.centerX;
            
        }
        [titleView addSubview:indicatorView];
    }
}
#pragma mark -设置所有按钮
- (void)setupChildVC
{
    YKAllViewController *all = [[YKAllViewController alloc] init];
    [self addChildViewController:all];
    YKVideoViewController *video = [[YKVideoViewController alloc] init];
    [self addChildViewController:video];
    YKPictureViewController *Picture = [[YKPictureViewController alloc] init];
    [self addChildViewController:Picture];
    YKVoiceViewController *Voice = [[YKVoiceViewController alloc] init];
    [self addChildViewController:Voice];
    YKWordViewController *Word = [[YKWordViewController alloc] init];
    [self addChildViewController:Word];
    
}
#pragma mark - 点击按钮
- (void)btnClick:(UIButton *)btn
{
    self.SelectButton.enabled = YES;
    btn.enabled = NO;
    self.SelectButton = btn;
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = btn.titleLabel.width;
        self.indicatorView.centerX = btn.centerX;
    }];
    // 滚动
    CGPoint offset = self.contentView.contentOffset;
    offset.x = btn.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}
- (void)setupNav
{
    UIButton *titleView = [[UIButton alloc] init];
    titleView.adjustsImageWhenHighlighted = NO;
    [titleView setBackgroundImage:[UIImage imageNamed:@"MainTitle"] forState:UIControlStateNormal];
    titleView.size = titleView.currentBackgroundImage.size;
    self.navigationItem.titleView = titleView;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"MainTagSubIcon" highImageName:@"MainTagSubIconClick" target:self action:@selector(TagSubIconClick)];
    self.view.backgroundColor = YKGlobalBg;

}
#pragma mark -
- (void)setupContentView
{
    UIScrollView *contentView = [[UIScrollView alloc] init];
    self.automaticallyAdjustsScrollViewInsets = NO;
    contentView.frame = self.view.bounds;
    self.contentView = contentView;
    
    contentView.delegate = self;
    
    contentView.pagingEnabled = YES;
    contentView.contentSize = CGSizeMake(YkScreeWidth * 5, 0);
    
    [self.view insertSubview:contentView atIndex:0];
    // 添加第一个控制器的view
    [self scrollViewDidEndScrollingAnimation:contentView];

  
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    UITableViewController *Vc = self.childViewControllers[index];
    
    Vc.view.x = scrollView.contentOffset.x;
    
    Vc.view.height = scrollView.height;
    Vc.view.y = 20;
    Vc.view.width = scrollView.width;
    
    CGFloat bottom = self.tabBarController.tabBar.height;
    CGFloat top = CGRectGetMaxY(self.titleView.frame);
    self.tabeleview.contentInset = UIEdgeInsetsMake(top, 0, bottom, 0);
    [scrollView addSubview:Vc.view];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
    
    // 点击按钮
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self btnClick:self.titleView.subviews[index]];

}
- (void)TagSubIconClick

{
    YKRecommendTagController *tag = [[YKRecommendTagController alloc] init];
    [self.navigationController pushViewController:tag animated:YES];
}

- (void)dealloc
{
   
    
}

@end
